/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom;

import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author François
 */
@Stateful
public class Cart implements CartLocal {

    @PersistenceContext(unitName = "BaseBoutique")
    private EntityManager em;

    private List<Product> caddie;

    public Cart() {
    }

    @Override
    public void addProduct(Long productPK) {
        caddie.add(em.find(Product.class, productPK));
    }

    @Override
    public List getProducts() {
        return caddie;
    }

    @Override
    public double getTotalPrice() {
        double total = 0;
        for (int i = 0; i < caddie.size(); i++) {
            total += caddie.get(i).getPrice();
        }
        return total;
    }

    @Override
    public void buy(String accountId) {
        em.find(Account.class, accountId).withdraw(getTotalPrice());
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
